package com.demo.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.LayoutRes;

import com.module.commonui.R;

public class BottomFullDialog extends Dialog {
    public void setOnTouchDismiss(boolean onTouchDismiss) {
        this.onTouchDismiss = onTouchDismiss;
    }

    private boolean onTouchDismiss = true;
    public BottomFullDialog(Context context) {
        super(context);
    }

    public BottomFullDialog(Context context, @LayoutRes int resource) {
        super(context, R.style.BottomFullDialog);
        View contentView = getLayoutInflater().inflate(
                resource, null);

        setLayoutViewEx(contentView);
    }

    private void setLayoutViewEx(View contentView) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        contentView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (onTouchDismiss){
                    BottomFullDialog.this.dismiss();
                }

                return true;
            }
        });
        super.setContentView(contentView);
    }

    public BottomFullDialog(Context context,  View contentView) {
        super(context, R.style.BottomFullDialog);
        setLayoutViewEx(contentView);
    }


    @SuppressWarnings("deprecation")
    public int getWidth() {
        WindowManager windowManager=getWindow().getWindowManager();
        Display display= windowManager.getDefaultDisplay();
        return display.getWidth();
    }

    public int getY(){
        return 0;
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setGravity(Gravity.BOTTOM);//设置显示在底部
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width=  getWidth();
        int y = getY();
        if (y > 0){
            layoutParams.y = y;
        }
        getWindow().setAttributes(layoutParams);
    }

}